#include "funcHdr.h"

#include <stdio.h>
#include <stdbool.h>

//------------------------------------------------------------------------------
// Pick one
//#define DEMO
//#define TINY
#define HTML

//------------------------------------------------------------------------------
// We will need access to the function header details
extern  Header  hdr;

//+=============================================================================
#ifdef HTML
void  hdrOutput (void)
{
	static  bool  prefix = false;

	int  i;

	if (!prefix) {
		char  *title = "csud";
		printf("<html>"
		         "<head>"
		           "<title>%s</title>"
		         "</head>"
		         "<body>"
		           "<center><table width=\"80%%\" border=\"1\">",
		       title);
	}

	// <!-- Function Definition -->
	printf("<tr><td bgcolor=\"#C0C0C0\"><table>"
	         "<tr>");
	if (!*hdr.argType[0])
		printf("<td></td><td>void</td><td>%s</td><td>(</td>", hdr.func);
	else
		printf("<td>%s</td><td>%s</td><td>%s</td><td>(</td>",
		       hdr.argQual[0], hdr.argType[0], hdr.func);
	// <!-- 0 args -->
	if      (hdr.argc == 0) {  
		printf("<td>void</td><td>)</td>");
	}
	// <!-- 1 arg -->
	else if (hdr.argc == 1) {  
		printf("<td>%s</td><td>%s</td><td>%s</td><td>)</td>",
		       hdr.argQual[1], hdr.argType[1], hdr.argName[1]);
	}
	// <!-- n args -->
	else {                     
		printf(  "<td>%s</td><td>%s</td><td>%s</td><td>&nbsp;</td>"
		       "</tr>",
		       hdr.argQual[1], hdr.argType[1], hdr.argName[1]);
		for (i = 2;  i < hdr.argc;  i++)
			printf("<tr>"
			         "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>"
			         "<td>%s</td><td>%s</td><td>%s</td><td>&nbsp;</td>"
			       "</tr>",
			       hdr.argQual[i], hdr.argType[i], hdr.argName[i]);
		printf("<tr>"
		         "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>"
		         "<td>%s</td><td>%s</td><td>%s</td><td>)</td>",
		       hdr.argQual[i], hdr.argType[i], hdr.argName[i]);
	}
	printf(  "<tr>"
	       "</table>");
	printf("%s", (*hdr.desc) ? hdr.desc : "<small>[Description Missing]</small>");

	printf("</td></<tr>\n");

/*
	// <!-- Description -->
	printf("<tr><td><u><b>Description</b></u></td></tr>");
	// <!-- Description missing -->
	if (!*hdr.desc)
		printf("<tr><td>-missing-</td></tr>");
	// <!-- Description present -->
	else
		printf("<tr><td>%s</td></tr>", hdr.desc);
*/

	// <!-- Arguments -->
	printf("<tr><td><u><b>Arguments</b></u></td></tr>");
	// <!-- 0 Args -->
	if (hdr.argc == 0) {
		printf("<tr><td>-none-</td></tr>");
	// <!-- 1 or more Args -->
	} else {
		printf("<tr><td><table>");
		// <!-- Each Arg -->
		for (i = 1;  i <= hdr.argc;  i++) // 1st arg in in [1] not [0]
			printf("<tr>"
			         "<td valign=\"top\">%s</td>"
			         "<td valign=\"top\">&nbsp;..&nbsp;</td>"
			         "<td>%s</td>"
			       "</tr>",
			       hdr.argName[i], hdr.argDesc[i]);
		printf("</table></td></tr>");
	}

	// <!-- Return Value -->
	printf("<tr><td><u><b>Return Value</b></u></td></tr>");
	// <!-- Return Value void -->
	if (!*hdr.argType[0]) {
		printf("<tr><td>-none-</td></tr>");
	// <!-- Return Value non-void -->
	} else {
		printf("<tr><td><table>"
		         "<tr>"
		           "<td valign=\"top\">%s</td>"
		           "<td valign=\"top\">&nbsp;..&nbsp;</td>"
		           "<td>%s</td></tr>",
		       hdr.argType[0], hdr.argDesc[0]);
		printf("</table></td></tr>");
	}

	// <!-- Notes -->
	printf("<tr><td><u><b>Notes</b></u></td></tr>");
	// <!-- 0 Notes -->
	if (hdr.ncnt == 0) {
		printf("<tr><td>-none-</td></tr>");
	// <!-- 1 or more Notes -->
	} else {
		printf("<tr><td><table>");
		for (i = 0;  i < hdr.ncnt;  i++)
			printf("<tr>"
			         "<td valign=\"top\">#</td>"
			         "<td valign=\"top\">%s</td>"
			       "</tr>",
			       hdr.note[i]);
		printf("</table></td></tr>");
	}

	// <!-- Authors -->
	printf("<tr><td><u><b>Author%s:</b></u> ",
	       (hdr.acnt > 1) ? "s" : "");
	// <!-- 0 Authors -->
	if (hdr.acnt == 0) {
		printf("-unknown-");
	//<!-- 1 or more Authors -->
	} else {
		for (i = 0;  i < hdr.acnt;  i++) {
			printf("%s", hdr.auth[i]);
			if (i != hdr.acnt - 1)  printf(" ;") ;
			printf(" ");
		}
	}
	printf("</td></tr>");

/*
	// <!-- Authors -->
	printf("<tr><td><u><b>Authors</b></u></td></tr>");
	// <!-- 0 Authors -->
	if (hdr.acnt == 0) {
		printf("<tr><td>-unknown-</td></tr>");
	//<!-- 1 or more Authors -->
	} else {
		printf("<tr><td>");
		for (i = 0;  i < hdr.acnt;  i++) {
			printf("%s", hdr.auth[i]);
			if (i != hdr.acnt - 1)  printf(" ;") ;
			printf(" ");
		}
		printf("</td></tr>");
	}
*/
	printf("</table></center><br /><br />\n");

	return;
}
#endif

//+=============================================================================
#ifdef TINY
void  hdrOutput (void)
{
	printf("%s%s:%-4d => %s\n", hdr.path, hdr.file, hdr.line, hdr.func);
}
#endif

//+=============================================================================
#ifdef DEMO
void  hdrOutput (void)
{
	int     i;

	printf("<PRE>//+=============================================================================\n");

	printf("Location: %s%s:%d\n", hdr.path, hdr.file, hdr.line);

	// Function Name
	printf("Func: |%s|\n", hdr.func);

	// Description
	printf("Desc: |%s|\n", hdr.desc);

	// Arguments
	printf("Args: ");
	if (!hdr.argc) {         // "-none-"
		printf("-none-\n");
	} else {                 // One-or-more Arguments
		printf("[%d]\n", hdr.argc);
		for (i = 1;  i <= hdr.argc;  i++)
			printf("    #%02d : |%s|%s|%s|%s|\n", i,
			       hdr.argQual[i], hdr.argType[i], hdr.argName[i], hdr.argDesc[i]);
	}

	// Return Value
	printf("Retn: ");
	if (!*hdr.argType[0])    // "-none-"
		printf("-none-\n");
	else                     // Return Value specified
		printf("|%s|%s|%s|\n", hdr.argQual[0], hdr.argType[0], hdr.argDesc[0]);

	// Notes
	printf("Note: ");
	if (!hdr.ncnt) {         // "-none-"
		printf("-none-\n");
	} else {                 // One-or-more Notes
		printf("[%d]\n", hdr.ncnt);
		for (i = 0;  i < hdr.ncnt;  i++)  printf("  # |%s|\n", hdr.note[i]) ;
	}

	// Authors
	printf("Auth: ");
	if (!hdr.acnt) {         // "_none-"
		printf("-none-\n");
	} else {                 // One-or-more Authors
		printf("[%d]", hdr.acnt);
		for (i = 0;  i < hdr.acnt;  i++) {
			if (!i)  printf("|") ;
			printf("%s|", hdr.auth[i]);
		}
	}
	printf("\n");

	return;
}
#endif
